package com.cos.hospital.ui.patient;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cos.hospital.R;
import com.cos.hospital.code.Code;
import com.cos.hospital.init.BaseActivity;
import com.cos.hospital.model.UHttp;
import com.cos.hospital.model.bean.ErrorBean;
import com.cos.hospital.model.bean.ResultBean;
import com.cos.hospital.model.bean.SurplusCureBean;
import com.cos.hospital.model.connector.HttpMsgListener;
import com.cos.hospital.model.event.PatientInfoEditEvent;
import com.cos.hospital.tool.SPUtil;
import com.cos.hospital.tool.USnackbar;
import com.cos.hospital.tool.UToast;
import com.cos.hospital.tool.UTxt;
import com.cos.hospital.ui.patient.adapter.CureCountAdapter;
import com.cos.hospital.widget.MyDividerDecoration;
import com.cos.hospital.widget.ProgressDialog;
import com.google.gson.JsonObject;
import com.jude.utils.JUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 剩余治疗项目
 */
public class SurplusCureActivity extends BaseActivity {
    @BindView(R.id.rc)
    RecyclerView rc;
    private BaseQuickAdapter adapter;
    private List<SurplusCureBean.ItemsBean> list;
    private int patientId;

    @Override
    protected int layout() {
        return R.layout.activity_surplus_cure;
    }

    @Override
    protected void init() {
        patientId = getIntent().getIntExtra(Code.SP.ID, 0);
        setTitleLeftIcon();
        setTitleCenterTxt("剩余治疗项目");
        setTitleRightTxt("保存");

        list = new ArrayList<>();
        adapter = new CureCountAdapter(list);
        adapter.openLoadAnimation();
        rc.setLayoutManager(new LinearLayoutManager(this));
        rc.addItemDecoration(new MyDividerDecoration(Color.TRANSPARENT, JUtils.dip2px(5)));
        rc.setAdapter(adapter);

    }

    @Override
    protected void logic() {
        adapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.iv_reduce:
                    dododo(adapter, position, false);
                    break;
                case R.id.iv_add:
                    dododo(adapter, position, true);
                    break;
            }
        });
        getSurplusCureList();
    }

    private void dododo(BaseQuickAdapter adapter, int position, boolean add) {
        EditText edt_count = (EditText) adapter.getViewByPosition(rc, position, R.id.edt_count);
        if (edt_count == null) {
            return;
        }
        String content = edt_count.getText() + "";
        int count;
        if (content.length() == 0) {
            count = 1;
        } else {
            count = Integer.parseInt(content);
        }
        if (add) {
            if (count > 99) {
                USnackbar.showError(context, "不能再加啦");
                return;
            }
            edt_count.setText(++count + "");
        } else {
            if (count < 1) {
                USnackbar.showError(context, "不能再减啦");
                return;
            }
            edt_count.setText(--count + "");
        }
    }

    private void getSurplusCureList() {
        ProgressDialog.getInstance().startProgressDialog(this);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("fldPatientId", patientId);
        UHttp.start(getServes().getSurplusCureList(jsonObject), "剩余患者治疗项目查询", new HttpMsgListener<ResultBean<SurplusCureBean>>() {
            @Override
            public void onSubscribe(Disposable disposable) {
                addEvent(disposable);
            }

            @Override
            public void onError(String describe) {
                USnackbar.showWarning(context, describe);
            }

            @Override
            public void onFinish() {
                ProgressDialog.getInstance().stopProgressDialog();
            }

            @Override
            public void onSuccess(ResultBean<SurplusCureBean> dto) {
                if (dto.isSuccess()) {
                    SurplusCureBean resultBean = dto.getResult();
                    if (resultBean != null) {
                        List<SurplusCureBean.ItemsBean> itemsBeanList = resultBean.getItems();
                        if (itemsBeanList != null && itemsBeanList.size() != 0) {
                            list.addAll(itemsBeanList);
                        }
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    ErrorBean errorBean = dto.getError();
                    if (errorBean != null) {
                        showNotice(UTxt.isNull(errorBean.getMessage(), Code.DefaultPrompt));
                    } else {
                        showNotice(Code.DefaultPrompt);
                    }
                }
            }
        });
    }

    @OnClick({R.id.title_back, R.id.title_next_txt})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.title_next_txt:
                save();
                break;
        }
    }

    private void save() {
        ProgressDialog.getInstance().startProgressDialog(this);
        Map<String, Object> map = new HashMap<>();
        map.put("fldEditId", SPUtil.getUserId());
        map.put("fldPatientId", patientId);
        map.put("patientItem", list);
        UHttp.start(getServes().curePatientAdd(map), "患者治疗项目修改增加", new HttpMsgListener<ResultBean>() {
            @Override
            public void onSubscribe(Disposable disposable) {
                addEvent(disposable);
            }

            @Override
            public void onError(String describe) {
                USnackbar.showWarning(context, describe);
            }

            @Override
            public void onFinish() {
                ProgressDialog.getInstance().stopProgressDialog();
            }

            @Override
            public void onSuccess(ResultBean dto) {
                if (dto.isSuccess()) {
                    UToast.toast("保存成功");
                    EventBus.getDefault().post(new PatientInfoEditEvent());
                    finish();
                } else {
                    ErrorBean errorBean = dto.getError();
                    if (errorBean != null) {
                        showNotice(UTxt.isNull(errorBean.getMessage(), Code.DefaultPrompt));
                    } else {
                        showNotice(Code.DefaultPrompt);
                    }
                }
            }
        });
    }
}
